//
//  ILHomeCarCollectionTableViewController.m
//  ILife
//
//  Created by lanou on 15/11/4.
//  Copyright © 2015年 ILife. All rights reserved.
//

#import "ILHomeCarCollectionTableViewController.h"
#import "FMDatabase.h"
#import "ILCarCollectionModel.h"
#import "ILCarCollectionTableViewCell.h"
#import "ILCarDetailViewController.h"

@interface ILHomeCarCollectionTableViewController ()

@property (nonatomic, strong) FMDatabase *dataBase;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ILHomeCarCollectionTableViewController

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
     self.navigationItem.title = @"汽车收藏";
    [self.tableView registerClass:[ILCarCollectionTableViewCell class] forCellReuseIdentifier:@"reuse"];
    self.tableView.rowHeight = SCREEN_WIDTH / 8 * 5 + 10;
    self.tableView.separatorStyle = UITableViewCellEditingStyleNone;
    // 创建数据库打开数据库
    _dataBase = [FMDatabase databaseWithPath:CARCOLLECTIONFILEPATH];
    
    if ([_dataBase open]) {
        NSLog(@"数据库打开成功");
    } else {
        NSLog(@"数据库打开失败");
    }
    
    FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"SELECT * FROM ICARCOLLEC"];
    while ([resultSet next]) {
        NSString *resourceLoc = [resultSet stringForColumn:@"resourceLoc"];
        NSString *picUrl = [resultSet stringForColumn:@"picUrl"];
        NSString *title = [resultSet stringForColumn:@"title"];
        ILCarCollectionModel *carModel = [[ILCarCollectionModel alloc] init];
        carModel.resourceLoc = resourceLoc;
        carModel.picUrl = picUrl;
        carModel.title = title;
        [self.dataArr addObject:carModel];
    }
    [resultSet close];
    if (![resultSet next]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有收藏呢 快去收藏一下吧！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ILCarCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    ILCarCollectionModel *model = self.dataArr[indexPath.row];
    
    cell.carModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ILCarDetailViewController *detailVC = [[ILCarDetailViewController alloc] init];
    ILCarCollectionModel *model = _dataArr[indexPath.row];
    detailVC.collectionModel = [[ILCarCollectionModel alloc] init];
    detailVC.collectionModel.resourceLoc = model.resourceLoc;
    [self.navigationController pushViewController:detailVC animated:YES];
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [_dataBase executeUpdate:@"DELETE FROM ICARCOLLEC WHERE resourceLoc = ?",((ILCarCollectionModel *)_dataArr[indexPath.row]).resourceLoc];
        [self.dataArr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
